package com.callback.confirmreturndemo.callback;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Configuration;

/**
 * 方式一 ， 方式二见  CallBackSendMessage.class
 */
@Slf4j
@Configuration
public class CallBackConfig implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnCallback {
    /**
     * 消息从生产者抵达交换机
     * @param correlationData correlation data for the callback.
     * @param ack true for ack, false for nack
     * @param cause An optional cause, for nack, when available, otherwise null.
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (ack){
            log.info("消息送达交换机 ："+correlationData.getId());

        }else {
            log.error("消息未送达交换机："+correlationData.getId());
            log.error("消息未送达交换机理由："+cause);
        }
    }

    /**
     * 消息从交换机抵达队列 ， 仅失败时
     * @param message the returned message.
     * @param replyCode the reply code.
     * @param replyText the reply text.
     * @param exchange the exchange.
     * @param routingKey the routing key.
     */
    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        log.error("消息未送达oqueue message:{},replyCode:{},replyText:{},exchange:{},routingKey:{}",
                message.getBody(),
                replyCode,
                replyText,
                exchange,
                routingKey);
    }
}
